// package-star/pages/brand-distribute-setting-product/index.js
const app = getApp();
import { PACKAGE_STAR_PAGE } from '../../../constants/route';
import { ACTIVITY_BONUS_RATE } from '../../../constants/activity';
import { navTo, tipToast } from '../../../common/env';
import { formatInputDecimal } from '../../../common/utils';
import { setDistributeProduct, setDistributeAllProduct } from '../../../api/brd';

Page({
    data: {
        settingInfo: {},
        allPercentOffModal: {
            show: false,
        },
        fromStep: false,
    },
    onLoad(options) {
        const { fromStep } = options;
        this.setData({ fromStep });
    },
    handleShoAllPercentOffModal() {
        this.setData({
            'allPercentOffModal.show': true,
        })
    },
    async handleChangeAllPercentOff(e) {
        const percentOff = formatInputDecimal(e.detail, 1);
        if (percentOff < 0) {
            tipToast('请输入正确的佣金比例');
            return;
        }
        if (percentOff > ACTIVITY_BONUS_RATE) {
            tipToast(`佣金比例最高不超过${ACTIVITY_BONUS_RATE}%`);
            return;
        }
        const flag = await setDistributeAllProduct({
            percentOff: percentOff * 10,
        });
        if (flag) {
            app.event.trigger('productListRefresh');
        }
    },
    async handleChangePercentOff(e) {
        const { productId, percentOff } = e.detail;
        const flag = await setDistributeProduct({
            productId,
            percentOff,
        });
        if (flag) {
            app.event.trigger('productListRefresh');
        }
    },
    handleComplete() {
        navTo(PACKAGE_STAR_PAGE['brandDistributor']);
    },
})